package com.rm.system.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.demo.blog.Blog;
import com.jfinal.ext.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;

public class User extends Model<User> {
	private static final long serialVersionUID = 5483298749400013942L;
	public static final User dao = new User();

	// // oracle支持创建User并使用序列
	// User user = new User().set("id", "MY_SEQ.nextval").set("age", 18);
	// user.save();

	public User findByName(final String loginName) {
		// User u = User.dao.findFirst(SqlKit.sql("system.user.findbyname"),
		// loginName);
		// 缓存用法
		User u = User.dao.findFirstByCache("system", "findByName_" + loginName,
				SqlKit.sql("system.user.findbyname"), loginName);

		// test
		// u.set("emp_code", "admin");
		// u.set("password", "1");
		return u;
	}

	public Collection<String> getRolesName(String loginName) {
		Collection<String> roles = new ArrayList<String>();
		// roles.add("admin");
		User u = findByName(loginName);
		long userid = u.getBigDecimal("userid").longValue();
		List<Role> rs = Role.dao.find(SqlKit.sql("system.user.getRoleNames"),
				userid);
		for (Role role : rs) {
			roles.add(role.getStr("role_name"));
		}
		return roles;
	}

	public String getName() {
		return get("emp_code");
	}

	public String getPwd() {
		return get("password");
	}

	/**
	 * 数据库没有该值，所以设置为0
	 * 
	 * @return
	 */
	public Object getStatus() {
		return "0";
	}

	public Object getId() {
		return get("userid");
	}
}
